/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.recption.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.List;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.recption.entity.RecptionReport;
import com.thinkgem.jeesite.modules.recption.service.RecptionReportService;
import com.thinkgem.jeesite.modules.shelvesreport.entity.ShelvesReport;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 收货报告Controller
 * @author zxl
 * @version 2017-08-04
 */
@Controller
@RequestMapping(value = "${adminPath}/recption/recptionReport")
public class RecptionReportController extends BaseController {

	@Autowired
	private RecptionReportService recptionReportService;
	@Autowired
	private OfficeService officeService;

	@ModelAttribute
	public RecptionReport get(@RequestParam(required=false) String id) {
		RecptionReport entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = recptionReportService.get(id);
		}
		if (entity == null){
			entity = new RecptionReport();
		}
		return entity;
	}
	
	@RequiresPermissions("recption:recptionReport:view")
	@RequestMapping(value = {"list", ""})
	public String list(RecptionReport recptionReport, HttpServletRequest request, HttpServletResponse response, Model model) {
		/*getWar(recptionReport);*/
		Page<RecptionReport> page = recptionReportService.findPage(new Page<RecptionReport>(request, response), recptionReport);
		/*recptionReport.setDestinationWhCode("");*/
		model.addAttribute("page", page);
		return "modules/recption/recptionReportList";
	}

	@RequiresPermissions("recption:recptionReport:view")
	@RequestMapping(value = "form")
	public String form(RecptionReport recptionReport, Model model) {
		model.addAttribute("recptionReport", recptionReport);
		return "modules/recption/recptionReportForm";
	}

	@RequiresPermissions("recption:recptionReport:edit")
	@RequestMapping(value = "save")
	public String save(RecptionReport recptionReport, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, recptionReport)){
			return form(recptionReport, model);
		}
		if(recptionReport.getPackageQty().equals(0)){
			recptionReport.setActuEntryWarhousenameTime(new Date());
		}
		recptionReportService.save(recptionReport);
		addMessage(redirectAttributes, "保存收货报告成功");
		return "redirect:"+Global.getAdminPath()+"/recption/recptionReport/?repage";
	}
	
	@RequiresPermissions("recption:recptionReport:edit")
	@RequestMapping(value = "delete")
	public String delete(RecptionReport recptionReport, RedirectAttributes redirectAttributes) {
		recptionReportService.delete(recptionReport);
		addMessage(redirectAttributes, "删除收货报告成功");
		return "redirect:"+Global.getAdminPath()+"/recption/recptionReport/?repage";
	}
	/**
	 * 导出数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("recption:recptionReport:view")
	@RequestMapping(value = "export")
	public String exportFile(RecptionReport shelvesReport, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
		/*	getWar(shelvesReport);*/
			String fileName = "收货报告"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			Page<RecptionReport>pageConfig = new Page<RecptionReport>(request, response, -1);
			pageConfig.setPageSize(65535);
			Page<RecptionReport> page = recptionReportService.findPage(pageConfig, shelvesReport);
			new ExportExcel("收货报告", ShelvesReport.class).setDataList(page.getList()).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出收货报告！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/shelvesreport/shelvesReport/?repage";
	}

	private void getWar(RecptionReport shelvesReport){
	/*	if(null ==shelvesReport || StringUtils.isEmpty(shelvesReport.getDestinationWhCode())){
			User user =  UserUtils.getUser();
			Office office = new Office();
			office.setParentIds(user.getOffice().getId());

			List<Office> all = officeService.findList(user.getOffice());
			List<String> warname = new ArrayList<>();
			warname.add(user.getOffice().getName());
			for(Office one:all){
				warname.add(one.getName());
			}
			shelvesReport.setDestinationWhCode("'"+StringUtils.join(warname,"','")+"'");
		}else if(StringUtils.isNotEmpty(shelvesReport.getDestinationWhCode())){
			shelvesReport.setDestinationWhCode("'"+StringUtils.join(shelvesReport.getDestinationWhCode(),"','")+"'");
		}*/
	}
}